//
//  RadarPictureViewController.m
//  carcareIOS
//
//  Created by wr on 16/2/24.
//  Copyright © 2016年 baozun. All rights reserved.
//

#import "RadarPictureViewController.h"

#import "HorizontalTableView.h"
#import "TrajectoryViewController.h"



@interface RadarPictureViewController ()<HorizontalTableViewDataSourse,UIScrollViewDelegate>

@property (nonatomic , strong) HorizontalTableView * hTableView;
@property (nonatomic , strong) HorizontalTableView * hHeadTableV;

@property (nonatomic , strong) UITableView * tableView;

//全天形式记录的modelArr 被点击的坐标
@property (nonatomic , assign) NSInteger selectIndex;//记录上一个页面传入的所选择的坐标

//数据值
//@property (nonatomic , strong) OneDriveModel * oneDriveModel;
@property (nonatomic , assign) NSInteger reloadSelectIndex;//记录这个页面滑动后的选择的坐标


@end

@implementation RadarPictureViewController
/**
 传入全天的行程记录数组 DRIVERSMGR.selelctDrivesModel.travelModels 所选中的index
 */
- (instancetype)initWithSelectIndex:(NSInteger)selectIndex;
{
    self = [super init];
    if (self) {
        self.selectIndex = selectIndex;
        self.reloadSelectIndex = selectIndex;
    }
    return self;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"单次行程";
    
    self.view.backgroundColor = [UIColor whiteColor];
    UIView * headV = [[UIView viewWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, TOP_HEIGHT)] setupOnView:self.view];
    [headV setClipsToBounds:YES];

    [[NSNotificationCenter defaultCenter] postNotificationName:DRIVE_PLACE_UPDATE object:nil];
    
    [headV addSubview:self.hHeadTableV];
    [self.hHeadTableV reloadData];
    
    [self.view addSubview:self.hTableView];
    [self.hTableView reloadData];
    [self.hTableView displayRow:self.selectIndex animation:NO];
    
    [self.hHeadTableV setContentOffset:CGPointMake((self.hTableView.contentOffset.x > 0 ? self.hTableView.contentOffset.x / 2 : 0)  - __MAIN_WIDTH / 2, 0)];
    

}


#pragma mark - horizontal datasource delegate

-(NSInteger)numberOfRowsWithHorizontalTableView:(HorizontalTableView *)horizontalTableView{

    return DRIVERSMGR.selelctDrivesModel.travelModels.count;
}

-(CGFloat)horizontalTableView:(HorizontalTableView *)horizontalTableView widthForRow:(NSInteger)row{
    if (horizontalTableView == self.hHeadTableV) {
        return __MAIN_WIDTH / 2;
    }
    return __MAIN_WIDTH;
}

-(HorizontalTableViewCell *)horizontalTableView:(HorizontalTableView *)horizontalTableView cellForRow:(NSInteger)row{
    if (horizontalTableView == self.hHeadTableV) {
        static NSString *cellID = @"headHCell";
        HorizontalTableViewCell * cell = [horizontalTableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[HorizontalTableViewCell alloc] initWithReuseIdentifier:cellID];
            UILabel * cellL = [[UILabel labelWithFrame:CGRectMake(0, 0, __MAIN_WIDTH / 2, TOP_HEIGHT) font:FONT_DEFAULT_Light(13) text:@"" textColor:[[UIColor whiteColor] colorWithAlphaComponent:0.3]] setupOnView:cell];
            cellL.tag = 10086;
            [cellL setTextAlignment:NSTextAlignmentCenter];
        }
        
        UILabel * cellL = (UILabel *)[cell viewWithTag:10086];
        
        [cellL setText:((OneDriveModel *)DRIVERSMGR.selelctDrivesModel.travelModels[row]).startAndEndTime];
        return cell;
    }
     static NSString *cellID = @"drives";
     RadarHorizontalCell * cell = [horizontalTableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[RadarHorizontalCell alloc] initWithReuseIdentifier:cellID];
    }
    cell.selectIndex = row;
    self.reloadSelectIndex = row;
     __weak __typeof(self) wself = self;
    cell.mapClick = ^()
    {
        OneDriveModel * model = DRIVERSMGR.selelctDrivesModel.travelModels[row];
        [wself.navigationController pushViewController:[[[TrajectoryViewController alloc]initWithDataModel:model] navSetupBackItemWithIdentification:BACK_ICON_TITLE] animated:YES];
    };
    return cell;
}

-(void)horizontalTableView:(HorizontalTableView *)horizontalTableView displayCells:(NSArray *)cells{
    
    
    if (cells.count > 0  && horizontalTableView == self.hHeadTableV) {
        
        CGFloat center = horizontalTableView.contentOffset.x + horizontalTableView.width / 2;
        CGFloat width = __MAIN_WIDTH / 2;
        CGFloat left = center - width / 2;
        CGFloat right = center + width / 2;
        for (RadarHorizontalCell * cell in cells) {
            UILabel * cellL = (UILabel *)[cell viewWithTag:10086];
            [cellL setTextColor: (cell.centerX >= left && cell.centerX <= right) ? [UIColor whiteColor] : [[UIColor whiteColor] colorWithAlphaComponent:0.3]];
        }
    }

}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (self.hTableView == scrollView) {
        [self.hHeadTableV setContentOffset:CGPointMake((self.hTableView.contentOffset.x > 0 ? (self.hTableView.contentOffset.x / __MAIN_WIDTH) * (__MAIN_WIDTH / 2) : 0) - __MAIN_WIDTH / 2, 0)];
    }else
    {
        [self.hTableView setContentOffset:CGPointMake((self.hHeadTableV.contentOffset.x + __MAIN_WIDTH / 2) / (__MAIN_WIDTH /2) * __MAIN_WIDTH, 0)];
    }
}


//#pragma mark - 上面黑色的透明view 添加手势
//- (void)tapGesture:(UITapGestureRecognizer *)tapGesture
//{
//    if (self.footerView.panGesClick) self.footerView.panGesClick();
//}

- (HorizontalTableView *)hTableView
{
    if (!_hTableView) {
        _hTableView = [[HorizontalTableView alloc]initWithFrame:CGRectMake(0, TOP_HEIGHT, __MAIN_WIDTH, __MAIN_HEIGHT_64 - TOP_HEIGHT)];
        _hTableView.dataSource = self;
        _hTableView.delegate = self;
        _hTableView.pagingEnabled = YES;
        _hTableView.showsHorizontalScrollIndicator = NO;
    }
    return _hTableView;
}
- (HorizontalTableView *)hHeadTableV
{
    if (!_hHeadTableV) {
        _hHeadTableV = [[HorizontalTableView alloc]initWithFrame:CGRectMake(- __MAIN_WIDTH / 4, 0, __MAIN_WIDTH  / 2 * 3, TOP_HEIGHT)];
        _hHeadTableV.contentInset = UIEdgeInsetsMake(0, __MAIN_WIDTH / 2, 0, __MAIN_WIDTH / 2) ;
        _hHeadTableV.backgroundColor = [Theme mainNavColor];
        _hHeadTableV.dataSource = self;
        _hHeadTableV.delegate = self;
        _hHeadTableV.pagingEnabled = YES;
//        _hHeadTableV.userInteractionEnabled = NO;
        _hHeadTableV.showsHorizontalScrollIndicator = NO;
    }
    return _hHeadTableV;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
